package com.ydles.system;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@SpringBootApplication
@EnableEurekaClient
public class SystemApplication {

    public static void main(String[] args) {
        SpringApplication.run(SystemApplication.class,args);
    }

    /**
     * 令牌桶,限制ip，在配置文件中写相关令牌配置
     *  每秒访问速率超过就返回 状态码429,给前端
     * @return
     */
    @Bean
    public KeyResolver ipKeyResolver(){
        return new KeyResolver() {
            //业务逻辑 说明限制说明
            @Override
            public Mono<String> resolve(ServerWebExchange exchange) {

                ServerHttpRequest request = exchange.getRequest();
                String ip = request.getRemoteAddress().getHostString();

                return Mono.just(ip); //仅仅限制ip
            }
        };
    }






}
